import http from '../utils/request';

/**
 * 用户登录
 * @param {string} username
 * @param {string} password
 */
export function login(username, password) {
  return http.post('/users/login', { username, password });
}

/**
 * 用户注册
 * @param {String} username
 * @param {String} password
 * @param {String} name
 * @param {String} email
 * @param {String} role
 * @param {Number} classId
 * @returns
 */
export function register(username, password, name, email, role, classId) {
  return http.post('/users/register', { username, password, name, email, role, classId });
}

/**
 * 获取当前用户信息
 * @returns
 */
export function getCurrentUser() {
  return http.get('/users/me');
}

/**
 * 更新用户信息
 * @param {String} name
 * @param {String} email
 * @returns
 */
export function updateCurrentUser(name, email) {
  return http.put('/users/profile', { name, email });
}

/**
 * 修改用户密码
 * @param {String} oldPassword
 * @param {String} newPassword
 * @returns
 */
export function changeCurrentPwd(oldPassword, newPassword) {
  return http.put('/users/change-password', { oldPassword, newPassword });
}

/*
-------------------
管理员接口
-------------------
*/

/**
 * 获取所有用户列表
 * @param {Number} page
 * @param {Number} limit
 * @param {String} role (admin|teacher|student)
 * @returns
 */
export function getAllUsers(page, limit, role) {
  return http.get('/users/all', { page, limit, role });
}

/**
 * 根据id获取用户信息
 * @param {Number} id
 * @returns
 */
export function getUserInfo(id) {
  return http.get(`/users/${id}`);
}

/**
 * 根据id更新用户信息
 * @param {Number} id
 * @param {String} username
 * @param {String} name
 * @param {String} email
 * @param {String} role
 * @param {Number} classId
 * @returns
 */
export function updateUser(id, username, name, email, role, classId) {
  return http.put(`/users/${id}`, { username, name, email, role, classId });
}

/**
 * 根据id重置用户密码
 * @param {Number} id
 * @param {String} newPassword
 * @returns
 */
export function resetPwd(id, newPassword) {
  return http.post(`/users/${id}/reset-password`, { newPassword });
}
